#!/usr/bin/env python
"""
Create a simple inputbox 

Copyright (c) 2008 Rune M. Friborg, runef@diku.dk.
See LICENSE.txt for licensing details (GPLv2).
"""

import wx
import sys

if len(sys.argv) != 4:
    sys.exit(0)

title = sys.argv[1]
msg = sys.argv[2]
value = sys.argv[3]

class BoxDialog(wx.Dialog):
    def __init__(
            self, parent, ID, title, msg, value, size=wx.DefaultSize, pos=wx.DefaultPosition, 
            style=wx.DEFAULT_DIALOG_STYLE
            ):

        # Instead of calling wx.Dialog.__init__ we precreate the dialog
        # so we can set an extra style that must be set before
        # creation, and then we create the GUI object using the Create
        # method.
        pre = wx.PreDialog()
        pre.Create(parent, ID, title, pos, size, style)

        # This next step is the most important, it turns this Python
        # object into the real wrapper of the dialog (instead of pre)
        # as far as the wxPython extension is concerned.
        self.PostCreate(pre)

        # Now continue with the normal construction of the dialog
        # contents
        sizer = wx.BoxSizer(wx.VERTICAL)
        box = wx.BoxSizer(wx.HORIZONTAL)

        label = wx.StaticText(self, -1, msg)
        box.Add(label, 0, wx.ALIGN_CENTRE|wx.ALL, 5)

        self.textCtrl = wx.TextCtrl(self, -1, value, size=(140,-1))
        box.Add(self.textCtrl, 1, wx.ALIGN_CENTRE|wx.ALL, 5)

        btn = wx.Button(self, wx.ID_OK)
        btn.SetDefault()
        box.Add(btn, 0, wx.ALIGN_RIGHT|wx.ALL, 5)

        sizer.Add(box, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
 
        self.SetSizer(sizer)
        sizer.Fit(self)
        
    def GetValue(self):
        return self.textCtrl.GetValue()

prevPos = None
app = wx.PySimpleApp()
ok = True
      
while ok:
    dlg = BoxDialog(None, -1, title, msg, value, size=(350, 200),
                         #style=wx.CAPTION | wx.SYSTEM_MENU | wx.THICK_FRAME,
                         style=wx.DEFAULT_DIALOG_STYLE # & ~wx.CLOSE_BOX,
                         )

    if prevPos != None:
        dlg.SetPosition(prevPos)

    if dlg.ShowModal() == wx.ID_OK:
        prevPos = dlg.GetPosition()
        value = dlg.GetValue()
        sys.stdout.write(value + '\n')
        sys.stdout.flush()
    
    else:
        ok = False
    dlg.Destroy()
